/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SIMPLE_PERF_THREAD_TREE_H_
#define SIMPLE_PERF_THREAD_TREE_H_

#include <limits.h>
#include <stdint.h>
#include <set>
#include "dso.h"

struct MapEntry {
  uint64_t start_addr;
  uint64_t len;
  uint64_t pgoff;
  uint64_t time;  // Map creation time.
  DsoEntry* dso;
};

struct MapComparator {
  bool operator()(const MapEntry* map1, const MapEntry* map2) const;
};

struct ThreadEntry {
  int pid;
  int tid;
  const char* comm;  // It always refers to the latest comm.
  std::set<MapEntry*, MapComparator> maps;
};

class ThreadTree {
 public:
  ThreadTree() : unknown_dso_(DSO_ELF_FILE, "unknown") {
    unknown_map_ = MapEntry{
        0,              // start_addr
        ULLONG_MAX,     // len
        0,              // pgoff
        0,              // time
        &unknown_dso_,  // dso
    };
    unknown_symbol_ = SymbolEntry{
        "unknown",   // name
        0,           // addr
        ULLONG_MAX,  // len
    };
  }

  void AddThread(int pid, int tid, const std::string& comm);
  void ForkThread(int pid, int tid, int ppid, int ptid);
  ThreadEntry* FindThreadOrNew(int pid, int tid);
  void AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff, uint64_t time,
                    const std::string& filename);
  void AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, uint64_t pgoff,
                    uint64_t time, const std::string& filename);
  const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip, bool in_kernel);
  const SymbolEntry* FindSymbol(const MapEntry* map, uint64_t ip);
  const MapEntry* UnknownMap() const {
    return &unknown_map_;
  }

 private:
  DsoEntry* FindKernelDsoOrNew(const std::string& filename);
  DsoEntry* FindUserDsoOrNew(const std::string& filename);

  std::unordered_map<int, std::unique_ptr<ThreadEntry>> thread_tree_;
  std::vector<std::unique_ptr<std::string>> thread_comm_storage_;

  std::set<MapEntry*, MapComparator> kernel_map_tree_;
  std::vector<std::unique_ptr<MapEntry>> map_storage_;
  MapEntry unknown_map_;

  std::unique_ptr<DsoEntry> kernel_dso_;
  std::unordered_map<std::string, std::unique_ptr<DsoEntry>> module_dso_tree_;
  std::unordered_map<std::string, std::unique_ptr<DsoEntry>> user_dso_tree_;
  DsoEntry unknown_dso_;
  SymbolEntry unknown_symbol_;
};

struct Record;

void BuildThreadTree(const Record& record, ThreadTree* thread_tree);

#endif  // SIMPLE_PERF_THREAD_TREE_H_
